/**
 * LSASlot.java
 *
 * @author Hagen Boehm
 */


package SSF.OS.OSPFv2;


import SSF.OS.ProtocolException;

// =============================== class SSF.OS.OSPFv2.LSASlot =============================== //
/**
 *  This class is intended as a container for a single LSA. It holds this LSA,
 *  and two markers indicating the install time and the age of the LSA when received. 
 */
public class LSASlot {

  public LSA lsa;
  // time at which the LSA was installed in this slot.
  public float installTime;
  // age of LSA when received.
  public long receivingAge;
  // A link to the OSPF session this Slot belongs to.
  public OSPF myOSPF;

  // ---------------------------------- constructor LSASlot -------------------------------------- //
  /** 
   * creates a new container for a single LSA. 
   *
   * @param lsa The first instance of the LSA installed in this slot.
   * @param time Timestamp indicating install time.
   * @param myOSPF A link to the OSPF session this slot belongs to.
   */
  public LSASlot(LSA lsa, OSPF myOSPF) {
    this.lsa = lsa;
    this.myOSPF = myOSPF;
    installTime = myOSPF.getSimTime();
    receivingAge = lsa.lsAge;
  }

  // ---------------------------------- LSASlot.install ------------------------------------- //
  /** installs a new instance of an existing LSA in this slot. Overridden in derived classes. */
  public void install(LSA adv) throws ProtocolException {
    lsa = adv;
    installTime = myOSPF.getSimTime();
    receivingAge = adv.lsAge;
  }

  // ------------------------------------ LSASlot.ageLSA ------------------------------------ //
  /** determines the new age of the LSA stored in this structure and set the lsAge field to the 
   *  new value. Return true if MAX_AGE is reached, else false.
   */
  public boolean ageLSA() {
    long newAge = receivingAge + (long)(myOSPF.getSimTime() - installTime);
    if (newAge >= myOSPF.MAX_AGE) {
      lsa.lsAge = myOSPF.MAX_AGE;
      return true;
    } else {
      lsa.lsAge = newAge;
      return false;
    }
  }

  // ------------------------------- LSASlot.lsaHasMaxAge ----------------------------------- //
  /** Check whether the LSA, stored in this slot, has lsAge equal to MAX_AGE.
   *
   *  @return boolean, holding true if stored LSA has age of MAX_AGE else false.
   */
  public boolean lsaHasMaxAge() {
    if (lsa.lsAge == myOSPF.MAX_AGE) return true;
    return false;
  }

  // --------------------------------- LSASlot.getLSAHeader --------------------------------- //
  /** Return a copy of the header of the LSA hold in this slot.
   */
  public LSA getLSAHeader() {
    return(lsa.copyHeader());
  }

  // ------------------------------------ LSASlot.getLSA ------------------------------------ //
  /** return a copy of the LSA hold in this slot.
   */
  public LSA getLSA() {
    return((LSA)lsa.copy());
  }

}





